package haut.system.member.person;

import haut.data.SystemData;
import haut.system.member.events.SportingEvents;
import haut.util.DataCheck;
import haut.util.UtilOfSportsEvents;
import haut.util.exception.DataException;

import java.util.Vector;

/**
 * <code>AbstractPerson</code>是一个人物抽象类，
 * 是所有系统使用者的父类。
 *
 *     待完善
 *
 * @author MingJie Li
 * */

abstract class AbstractPerson {

    /**
     * 工/学号
     * */
    protected String id;
    /**
     * 姓名
     * */
    protected String name;

    /**
     * 系统数据
     */
    protected SystemData data = SystemData.getInstance();

    /**
     * <code>AbstractPerson</code>的有参构造函数，
     * 创建对象并对赋予其 工/学号 以及 姓名
     * @param id 工/学号
     * @param name 姓名
     * */
    public AbstractPerson(String id, String name) throws DataException {
        DataCheck.personCheck(id, name);
        this.id = id;
        this.name = name;
    }

    /**
     * 查看当前项目
     */
    final public void checkAllEvents() {

        Vector<SportingEvents> allEvents = data.getAllEvents();
        SportingEvents event;
        int size = allEvents.size();

        for (SportingEvents allEvent : allEvents) {
            event = allEvent;
            System.out.println(event.getName());
        }
    }

    /**
     * 查看某项目的排名
     * @param id 项目编号
     */
    final public void checkAllRankOfEvent(String id) {
        SystemData data = SystemData.getInstance();
        Vector<SportingEvents> allEvents = data.getAllEvents();
        SportingEvents event = null;

        for(SportingEvents events : allEvents) {
            if(id == events.getId()) {
                event = events;
                break;
            }
        }
        if(event == null) {
            System.out.println("该项目不存在！");
            return;
        }

        Vector<Player> players = UtilOfSportsEvents.ScoreSort(event);
        int rank = 1;
        for(Player Iterator : players) {
            System.out.print(Iterator.id + "    " + Iterator.getName() + "    ");
            Iterator.checkMyScoreOfEvent(event.getId());
            System.out.println();
        }
    }

    /**
     * 查看某项目的得分情况
     * @param id 项目编号
     */
    final public void checkAllScoreOfEvent(String id) {
        checkAllRankOfEvent(id);
    }

    /**
     * 返回对象的工/学号
     * 该方法不允许被重写
     * @return id
     * */
    final public String getId() {
        return id;
    }

    /**
     * 返回对象的姓名
     * 该方法不允许被重写
     * @return name
     * */
    final public String getName() {
        return name;
    }

    /**
     * 输出显示本人信息
     * */
    public void print() {
        System.out.print(getId() + " " + getName());
    }

    /**
     * 本人是否是运动员
     * @return {boolean}
     */
    public boolean isPlayer() {
        SystemData data = SystemData.getInstance();
        Vector<Player> allPlayer = data.getAllPlayer();
        int size = allPlayer.size();
        for(int i = 0; i < size; ++i) {
            if(getId() == allPlayer.elementAt(i).getId())
                return true;
        }
        return false;
    }

    /**
     * 本人是否是裁判员
     * @return {boolean}
     */
    public boolean isJudge() {
        SystemData data = SystemData.getInstance();
        Vector<Judge> allJudges = data.getAllJudges();
        int size = allJudges.size();
        for(int i = 0; i < size; ++i) {
            if(getId() == allJudges.elementAt(i).getId())
                return true;
        }
        return false;
    }

    /**
     * 本人是否是志愿者
     * @return {boolean}
     */
    public boolean isVolunteer() {
        SystemData data = SystemData.getInstance();
        Vector<Volunteer> allVolunteer = data.getAllVolunteer();
        int size = allVolunteer.size();
        for(int i = 0; i < size; ++i) {
            if(getId() == allVolunteer.elementAt(i).getId())
                return true;
        }
        return false;}
}
